<template>
  <!-- 设备台账 -->
  <Layout>
       <Form ref="form1" :label-width="120">
        <Row  v-show="!switchFlag" class="query-form" style="padding-top: 15px">
            <Col span="8">
                <FormItem label="人员名称：">
                    <Input class="query-form-el" type="text" v-model="eqOperatorName"></Input>
                </FormItem>
            </Col>
            <Col span="8" offset="8">
                <Button @click="reset" class="ivu-btn-reset"  >重置</Button>
                <Button type="primary" class="ivu-btn-query"   icon="md-search"  @click="query">查询</Button>
            </Col>
        </Row>
        <Row  v-show="switchFlag" class="query-form" style="padding-top: 8px">
            <Col span="8">
                <FormItem label="人员名称：">
                    <Input class="query-form-el" type="text" v-model="eqOperatorName"></Input>
                </FormItem>
            </Col>
        </Row>
        <Row v-show="switchFlag" class="query-form">
            <Col span="24">
                <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text"  >收起∧</Button>
                <Button @click="reset" class="ivu-btn-reset"   >重置</Button>
                <Button type="primary" class="ivu-btn-query"   icon="md-search" @click="query">查询</Button>
            </Col>
        </Row>
        <Row class="query-form">
            <Col span="24">
                <Button size="large"  class="ivu-btn-add" icon="md-add"  @click="addInfo">新增</Button>
                <!--<Button size="large"  class="ivu-btn-delete" icon="md-trash" @click="deleteInfo">删除</Button>-->
            </Col>
        </Row>
      </Form>
     
      <Table ref='table' :height="tableHeight" :loading="loading" size="small" :row-class-name="rowClassName" no-data-text="没有查询到相关数据!"
           stripe border :columns="columns1"  @on-selection-change = 'getSelectRows' :data="resultData">
      </Table>
      <!-- 底部分页条 -->
    <Row style="margin-right:10px;">
        <Col span="24" >
                <Page style="float: right" 
                :total="dataCount" 
                :page-size="pageSize" 
                @on-change="changepage"
                 @on-page-size-change="nowPageSize" 
                 :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
        </Col>
    </Row>

    <Modal  title="操作人员" :transfer=false v-model="model1"  :width=600>
        <Row>
            <Col span="24">
                <Form ref="formCustom" :model="formCustom" :rules="ruleValidate"  label-position="top">
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="人员名称" prop="eqOperatorName">
                                <Input type="text" v-model="formCustom.eqOperatorName"></Input>
                            </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="身份证号" prop="eqCardNum">
                                <Input type="text" v-model="formCustom.eqCardNum"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="学历" prop="eqEducation">
                                 <Select v-model="formCustom.eqEducation" >
                                    <Option value="小学">小学</Option>
                                    <Option value="初中">初中</Option>
                                    <Option value="高中">高中</Option>
                                    <Option value="专科">专科</Option>
                                    <Option value="本科">本科</Option>
                                    <Option value="研究生">研究生</Option>
                                    <Option value="博士">博士</Option>
                                </Select>
                            </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="证件到期时间" prop="expireDate">
                                <DatePicker type="date"   v-model="formCustom.expireDate" class="query-form-el"  format="yyyy-MM-dd"  placeholder="请选择" ></DatePicker>
                            </FormItem>
                        </Col>
                    </Row>
                     <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23">
                            <FormItem label="上传证件：">
                                <Upload
                                        ref="upload"
                                        type="drag"
                                        :show-upload-list="true"
                                        :before-upload="beforeUpload"
                                        :action="upUrl"
                                        :max-size="10240"
                                    >
                                    <div style="padding: 20px 0">
                                        <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
                                        <p>点击或拖拽文件到这上传</p>
                                    </div>
                                </Upload>
                                <div v-if="formCustom.fileName !== null">
                                    附件名称: {{ formCustom.fileName }}
                                </div>
                            </FormItem>
                        </Col>
                    </Row>
                   
                    <!-- <Row type="flex" justify="center" class="code-row-bg">
                            <Col span="23" >
                                <FormItem>
                                    <Button type="primary"  class='ivu-btn-save' :loading="buttonLoading" @click="submit()" >确定</Button>
                                    <Button type="primary"  class='ivu-btn-cancel' @click="handleCancel" >取消</Button>
                                </FormItem>
                            </Col>
                    </Row>-->
                </Form>
            </Col>
        </Row>
        <div slot="footer">
            <Button type="primary"  class='ivu-btn-save' :loading="buttonLoading" @click="submit()" >确定</Button>
            <Button type="primary"  class='ivu-btn-cancel' @click="handleCancel" >取消</Button>
        </div>
    </Modal>

    <Modal title="操作人员" class="modal-view-detail" :transfer=false  v-model="model2"  :width=800 >
        <Row>
            <Col span="24">
                <Form  :model="showdata" :label-width="160" >
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="人员名称" prop="eqOperatorName">
                                {{showdata.eqOperatorName}}
                            </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="身份证号" prop="cardNum">
                                {{showdata.cardNum}}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="学历" prop="shortnameEn">
                                {{showdata.education}}
                            </FormItem>
                        </Col>
                        <Col span="11">
                           
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                         <FormItem label="文件" prop="file">
                                附件
                         </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="到期日期" prop="expireDate">
                                {{showdata.expireDate}}
                            </FormItem>
                        </Col>
                    </Row>
                   <!-- <Row>
                        <Col span="23" >
                            <FormItem>
                                <Button class='ivu-btn-save' @click="model2=false">关闭</Button>
                            </FormItem>
                        </Col>
                    </Row>-->
                </Form>
            </Col>
        </Row>
        <div slot="footer">
            <Button type="primary"  class='ivu-btn-save'  @click="model2=false">关闭</Button>
        </div>
    </Modal>
  </layout>

</template>

<script>
    import {
        Api
    } from "../../../../api/index.js"
    export default {
        data() {
            return {
                switchFlag: false,
                upUrl: Api.uploadEqOperatorFile,
                file: null,
                formCustom: {},
                eqOperatorName: '',
                tableHeight: 450,loading: false,
                styleArr: [{
                    lineHeight: '30px',
                    width: '220px'
                }],
                divStyle: {
                    marginTop: '20px'
                },
                btn: {
                    width: '80px',
                    height: '36px',
                    color: '#fff',
                    borderRadius: '4px',
                    border: '1px solid #dcdee2',
                    textAlign: 'center',
                    verticalAlign: 'middle',
                    marginLeft: '10px',
                },
                pageSizeArray: [5, 10, 20, 50],
                dataCount: 0,
                pageSize: 5,
                currentPage: 1,
                columns1: [ {
                    title: "序号",
                    width: 80,
                    align: "center",
                    fixed:'left',
                    render: (h, params) => {
                        return h('span', params.index + (this.currentPage - 1) * this.pageSize + 1);
                    }
                },
                    {
                        title: '人员名称',
                        align: 'center',
                        key: 'eqOperatorName'
                    },
                    {
                        title: '身份证号',
                        key: 'eqCardNum',
                        align: 'center'
                    },
                    {
                        title: '学历',
                        align: 'center',
                        key: 'eqEducation'
                    },
                    {
                        title: '证件',
                        align: 'center',
                        key: 'fileName',
                        render:(h,params)=>{
                            return h('div',[
                                h('a', {
                                    attrs: {
                                        href: Api.downloadOperatorFile + "/" +params.row.fileId
                                        }
                                    }, '下载')
                            ])
                        }
                    },
                    {
                        title: '到期时间',
                        align: 'center',
                        key: 'expireDate'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                             this.showData(params,2);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px',
                                        color:'red'
                                    },
                                    on: {
                                        click: () => {
                                            this.deleteData(params);
                                        }
                                    }
                                }, '删除'),
                            ]);
                        }
                    }
				],
                isModifyFile: false,
                showdata: {},
                model1: false,
                buttonLoading: false,model2: false,
                add: false,
                select: {},
                resultData: [],
                deleteRows: [],
                ruleValidate: {
                    eqCardNum: [
                    { required: true, message: "", trigger: "blur" }
                    ],
                    eqOperatorName: [
                    { required: true, message: "", trigger: "blur" }
                    ],
                    expireDate: [
                    { required: true, message: "", type:"date" , trigger: "blur" }
                    ],
                    eqEducation: [
                    { required: true, message: "", trigger: "blur" }
                    ]
                 }
            }
        },
        mounted: function() {
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            //页面加载执行的查询函数,返回的总条数,激活前端分页插件的页码功能.
            this.query();
        },
        methods: {
            switchQuery(flag){
                this.switchFlag = flag;
                if(!this.switchFlag){
                    this.tableHeight = window.innerHeight - 220;
                }else{
                    this.tableHeight = window.innerHeight - 270;
                }
            },
            modalcancel() {
                this.formCustom = {};
                this.model = false;
            },
            beforeUpload(file) {
                this.file = file;
                this.formCustom.fileName = file.name;
                if(this.formCustom.eqOperatorId != "")
                    this.isModifyFile = true;
                return false;
            },
            addInfo() {
                this.formCustom = {
                    eqOperatorId: '',
                    eqOperatorName: '',
                    eqCardNum: '',
                    eqEducation: '',
                    expireDate: null,
                    fileId: '',
                    fileName: null
                }
                this.isModifyFile = false;
                this.model1 = true;
            },
            //重置输入框
            reset: function() {
                this.eqOperatorName = '';
            },
            handleCancel() {
                this.formCustom = {};
                this.model1 = false;
            },
            //添加
            submit() {
                this.$refs["formCustom"].validate((valid) => {
                    if (valid) { this.buttonLoading=true
                        if(this.isModifyFile){
                            this.$http.delete(Api.deleteOperatorFile+"/"+this.formCustom.fileId).then(res=>{
                                this.formCustom.fileId = "";
                                if(this.file != null){
                                    let formData = new FormData();
                                    formData.append("file", this.file);
                                    this.$http.put(Api.uploadEqOperatorFile, formData).then((res) => {
                                        if (res.data.result == "success") {
                                            this.formCustom.fileId = res.data.id;
                                            this.formCustom.fileName = res.data.fileName;
                                            this.submitEqOperator();
                                        }
                                    })
                                }
                                else{
                                    this.submitEqOperator();
                                }     this.buttonLoading=false
                            });
                        }else{
                            if(this.file != null){
                                let formData = new FormData();
                                formData.append("file", this.file);
                                this.$http.put(Api.uploadEqOperatorFile, formData).then((res) => {
                                     console.log(res)
                                    if (res.data.result == "success") {
                                        this.formCustom.fileId = res.data.id;
                                        this.formCustom.fileName = res.data.fileName;
                                        this.submitEqOperator();
                                    }
                                })
                            }
                            else{
                                this.submitEqOperator();
                            }     this.buttonLoading=false
                        }
                    }
                });
            },
            submitEqOperator: function(){
                this.formCustom.expireDate = this.formatDate(new Date(this.formCustom.expireDate), 'yyyy-MM-dd');
                if(this.formCustom.eqOperatorId == ""){
                    this.$http.post(Api.operatorUrl, this.formCustom).then((res) => {
                        if (res.data.status == 200) {
                            this.query();
                            this.model1 = false;
                            this.$Message.info('添加成功');
                        } else {
                            this.$Message.info(res.data.message);
                        }
                    });
                }else{
                    this.$http.put(Api.operatorUrl, this.formCustom).then((res) => {
                        if (res.status == 200) {
                            this.query();
                            this.model1 = false;
                            this.$Message.info('修改成功');
                        } else {
                            this.$Message.info(res.data.message);
                        }
                    });
                }
            },
            formatDate(date, fmt) {
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
                }
                let o = {
                    'M+': date.getMonth() + 1,
                    'd+': date.getDate(),
                    'h+': date.getHours(),
                    'm+': date.getMinutes(),
                    's+': date.getSeconds()
                };
                for (let k in o) {
                    if (new RegExp(`(${k})`).test(fmt)) {
                        let str = o[k] + '';
                        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
                    }
                }
                return fmt;
            },
            padLeftZero(str) {
                return ('00' + str).substr(str.length);
            },
            showData(data, flag) {
            //修改页面
                this.isModifyFile = false;
                this.formCustom = data.row;
                this.model1 = true;
            },
            getSelectRows (rows){
                this.deleteRows = rows;
            },
            getInfo(){this.loading = true ;
                if (this.eqOperatorName == undefined) {
                    this.eqOperatorName = "";
                }
                this.$http.get(Api.queryEqOperator + "?starPage=" + this.currentPage + "&size=" + this.pageSize + 
                "&eqOperatorName=" + this.eqOperatorName).then((res) => {
                    this.dataCount = res.data.total; //总条数
                    this.resultData = res.data.data; //查询到的所有编码
                    this.loading = false;
                })
            },
            deleteInfo(){
                let eqOperatorId = '';
                console.log(this.deleteRows)
                this.deleteRows.forEach(item=>{
                    eqOperatorId = eqOperatorId  + item.eqOperatorId + ";"
                    this.$http.delete(Api.deleteOperatorFile+"/"+item.fileId).then(res=>{})
                });
                eqOperatorId = eqOperatorId.substring(0, eqOperatorId.length - 1);
                this.$http.delete(Api.operatorUrl + '/' + eqOperatorId).then((res) => {
                        if (res.status == 200) {
                            this.$Message.info('删除成功');
                            this.query();
                        } else {
                            this.$Message.info('删除失败');
                        }
                    });
            },
            deleteData(params) {
                 let istrue = confirm("是否确定删除?");
                 if (istrue) {
                    this.$http.delete(Api.deleteOperatorFile+"/"+params.row.fileId).then(res=>{})
                    this.$http.delete(Api.operatorUrl + '/' + params.row.eqOperatorId).then((res) => {
                        if (res.status == 200) {
                            this.$Message.info('删除成功');
                            this.query();
                        } else {
                            this.$Message.info('删除失败');
                        }
                    });
                }
            },
            query() {
                this.dataCount = 0;
                this.currentPage = 1;
                this.getInfo();
            },
            changepage(pageIndex) {
                //当页码改变,选择获取当前页数,发送查询请求
                this.currentPage = pageIndex;
                this.getInfo();
            },
            nowPageSize(size) {
                this.pageSize = size;
                this.currentPage = 1;
                this.getInfo();
            },
            rowClassName(row, index) {
                return 'tdHeight';
            }
        }
    }
</script>

<style scoped lang="css">
    @import '../../../../index.css';
</style>
